//
//  DiscoverTableViewCellFourth.m
//  XYFMApp
//
//  Created by 岁变 on 16/8/31.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import "DiscoverTableViewCellFourth.h"
#import "XYMacro.h"

@interface DiscoverTableViewCellFourth () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray *modelArr;

@end

@implementation DiscoverTableViewCellFourth


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _backImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"find_cat_header"]];
        
        [self.contentView addSubview:_backImageView];
        
        _typeLabel = [[UILabel alloc] init];
        _typeLabel.text = @"为您推荐";
        _typeLabel.textColor = [UIColor whiteColor];
        _typeLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:_typeLabel];
        
        _moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_moreButton setTitle:@"换一批" forState:UIControlStateNormal];
        [_moreButton setTitleColor:[UIColor colorWithRed:0.46 green:0.47 blue:0.50 alpha:1.00] forState:UIControlStateNormal];
        [_moreButton addTarget:self action:@selector(chang:) forControlEvents:UIControlEventTouchUpInside];
        _moreButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:_moreButton];
        
        _lineView = [[UIView alloc] initWithFrame:CGRectZero];
        _lineView.backgroundColor = [UIColor colorWithRed:1.00 green:0.27 blue:0.12 alpha:1.00];
        [self.contentView addSubview:_lineView];
        
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor colorWithRed:0.08 green:0.09 blue:0.12 alpha:1.00];
        [self.contentView addSubview:_collectionView];
        [_collectionView registerClass:[DiscoverCollectionViewCellOfContentViewFifth class] forCellWithReuseIdentifier:@"DiscoverCollectionViewCellOfContentViewFifth"];
        
    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 4;
}

- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    DiscoverCollectionViewCellOfContentViewFifth *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscoverCollectionViewCellOfContentViewFifth" forIndexPath:indexPath];
    cell.model = [_modelArr objectAtIndex:indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    //响应者
    
    VCOfDiscover *discoverVC = (VCOfDiscover *)self.nextResponder.nextResponder.nextResponder.nextResponder.nextResponder.nextResponder.nextResponder;
    VCOfDiscoverList *discoverListVC = [[VCOfDiscoverList alloc] init];
    
    DiscoverModelFirstRecommend *model = [_modelArr objectAtIndex:indexPath.item];
    discoverListVC.item_id = [NSString stringWithFormat:@"%ld", model.item_id];
    [discoverVC.navigationController pushViewController:discoverListVC animated:YES];
}



- (void)setModel:(DiscoverModelFirstData *)model {
    _model = [[DiscoverModelFirstData alloc] init];
    _model = model;
    _modelArr = [NSMutableArray array];
    NSInteger indexTwo = 0;
    NSInteger indexThree = 0;
    NSInteger indexFour = 0;
    NSInteger indexOne = arc4random() % _model.best_items.count;
    [_modelArr addObject:[_model.best_items objectAtIndex:indexOne]];
    
    do {
        indexTwo = arc4random() % _model.best_items.count;
    } while (indexTwo == indexOne);
    [_modelArr addObject:[_model.best_items objectAtIndex:indexTwo]];
    
    do {
        indexThree = arc4random() % _model.best_items.count;
    } while (indexThree == indexTwo || indexThree == indexOne);
    [_modelArr addObject:[_model.best_items objectAtIndex:indexThree]];
    
    do {
        indexFour = arc4random() % _model.best_items.count;
    } while (indexFour == indexThree || indexFour == indexTwo || indexFour == indexOne);
    [_modelArr addObject:[_model.best_items objectAtIndex:indexFour]];
    
}

- (void)chang:(UIButton *)button {
    [_modelArr removeAllObjects];
    NSInteger indexTwo = 0;
    NSInteger indexThree = 0;
    NSInteger indexFour = 0;
    NSInteger indexOne = arc4random() % _model.best_items.count;
    [_modelArr addObject:[_model.best_items objectAtIndex:indexOne]];

    do {
        indexTwo = arc4random() % _model.best_items.count;
    } while (indexTwo == indexOne);
    [_modelArr addObject:[_model.best_items objectAtIndex:indexTwo]];

    do {
        indexThree = arc4random() % _model.best_items.count;
    } while (indexThree == indexTwo || indexThree == indexOne);
    [_modelArr addObject:[_model.best_items objectAtIndex:indexThree]];

    do {
        indexFour = arc4random() % _model.best_items.count;
    } while (indexFour == indexThree || indexFour == indexTwo || indexFour == indexOne);
    [_modelArr addObject:[_model.best_items objectAtIndex:indexFour]];
    [self.collectionView reloadData];

}



- (void)layoutSubviews {
    
    [super layoutSubviews];
    [_backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(10);
        make.left.equalTo(self.contentView.mas_left).with.offset(5);
        make.width.mas_offset(30);
        make.height.mas_offset(30);
    }];
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(39);
        make.left.equalTo(self.contentView.mas_left).with.offset(15);
        make.right.equalTo(self.contentView.mas_right).with.offset(-15);
        make.height.mas_offset(1);
    }];
    
    [_typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(10);
        make.left.equalTo(_backImageView.mas_right).with.offset(5);
        make.width.mas_offset(200);
        make.height.mas_offset(28);
        
    }];
    
    [_moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(10);
        make.right.equalTo(self.contentView.mas_right).with.offset(-15);
        make.height.mas_offset(28);
        make.width.mas_offset(50);
    }];
    
    _layout.itemSize = CGSizeMake((SCREEN_WIDTH - 60) / 2, 430 / 2);
    _layout.minimumLineSpacing = 30;
    _layout.minimumInteritemSpacing = 10;
    _layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(50);
        make.left.equalTo(self.contentView.mas_left).with.offset(0);
        make.right.equalTo(self.contentView.mas_right).with.offset(0);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-10);
    }];

}


@end
